﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using QLTuyenXe.DAL;
using System.Collections;
using System.Data;
using QLTuyenXe.DAO;

namespace QLTuyenXe.BLL
{
    class BGroup
    {
        MyConnectUnit myConnU = new MyConnectUnit();

        public ArrayList Select(String columns, String condition, String order)
        {
            ArrayList groups = new ArrayList();
            DataTable tb = new DataTable();
            tb = myConnU.Select("NHOM", columns, condition, order);
            if (tb != null)
                foreach (DataRow r in tb.Rows)
                {
                    GROUP g = new GROUP();
                    g.MaNhom = r["maNhom"].ToString();
                    g.TenNhom = r["tennhom"].ToString();
                    g.ChuThich = r["chuthich"].ToString();
                    g.PhanQuyen = r["phanquyen"].ToString();
                    g.DoiMatKhau = r["doimatkhau"].ToString();
                    g.CongTy = r["congty"].ToString();
                    g.SaoLuu = r["saoluu"].ToString();
                    g.PhucHoi = r["phuchoi"].ToString();
                    g.Nhom = r["nhom"].ToString();
                    g.DieuHanh = r["dieuhanh"].ToString();
                    g.QuanLyXe = r["quanlyxe"].ToString();
                    g.LaiXe = r["laixe"].ToString();
                    g.TuyenXe = r["tuyenxe"].ToString();
                    g.XeThue = r["xethue"].ToString();
                    g.DoanhThu_XeChayTuyen = r["doanhthu_xechaytuyen"].ToString();
                    g.DoanhThu_XeThue = r["doanhthu_xethue"].ToString();
                    g.TaoHopDong = r["taohopdong"].ToString();
                    g.XuatBaoCao = r["xuatbaocao"].ToString();
                    g.HT_Them = r["ht_them"].ToString();
                    g.HT_Sua = r["ht_sua"].ToString();
                    g.HT_Xoa = r["ht_xoa"].ToString();
                    g.DanhMuc_Them = r["danhmuc_them"].ToString();
                    g.DanhMuc_Sua = r["danhmuc_sua"].ToString();
                    g.DanhMuc_Xoa = r["danhmuc_xoa"].ToString();
                    g.ThemKH = r["themKH"].ToString();
                    g.DSKH = r["danhsachKH"].ToString();
                    g.SuaKH = r["suaKH"].ToString();
                    g.XoaKH = r["xoaKH"].ToString();
                    groups.Add(g);
                }
            return groups;
        }

        public ArrayList Select(String columns, String condition)
        {
            return this.Select(columns, condition, null);
        }

        public ArrayList Select(String columns)
        {
            return this.Select(columns, null);
        }

        public ArrayList Select()
        {
            return this.Select("*");
        }

        public DataTable KTXoa(string dk)
        {
            ArrayList a = new ArrayList();
            DataTable tb = new DataTable();
            tb = myConnU.Select("NGUOI_DUNG", "NHOM", "NGUOI_DUNG.maNhom", "NHOM.maNhom", dk);
            return tb;
        }

        public int Insert(GROUP group)
        {
            ColumnData maNhom = new ColumnData("maNhom", 1, group.MaNhom.ToString());
            ColumnData tenNhom = new ColumnData("tennhom", 1, group.TenNhom.ToString());
            ColumnData chuThich = new ColumnData("chuthich", 1, group.ChuThich.ToString());
            ColumnData phanQuyen = new ColumnData("phanquyen", 1, group.PhanQuyen.ToString());
            ColumnData doiMatKhau = new ColumnData("doimatkhau", 1, group.DoiMatKhau.ToString());
            ColumnData congTy = new ColumnData("congty", 1, group.CongTy.ToString());
            ColumnData saoLuu = new ColumnData("saoluu", 1, group.SaoLuu.ToString());
            ColumnData phucHoi = new ColumnData("phuchoi", 1, group.PhucHoi.ToString());
            ColumnData nhom = new ColumnData("nhom", 1, group.Nhom.ToString());
            ColumnData dieuHanh = new ColumnData("dieuhanh", 1, group.DieuHanh.ToString());
            ColumnData quanLyXe = new ColumnData("quanlyxe", 1, group.QuanLyXe.ToString());
            ColumnData laiXe = new ColumnData("laixe", 1, group.LaiXe.ToString());
            ColumnData tuyenXe = new ColumnData("tuyenxe", 1, group.TuyenXe.ToString());
            ColumnData xeThue = new ColumnData("xethue", 1, group.XeThue.ToString());
            ColumnData doanhThu_Tuyen = new ColumnData("doanhthu_xechaytuyen", 1, group.DoanhThu_XeChayTuyen.ToString());
            ColumnData doanhthu_Thue = new ColumnData("doanhthu_xethue", 1, group.DoanhThu_XeThue.ToString());
            ColumnData taoHD = new ColumnData("taohopdong", 1, group.TaoHopDong.ToString());
            ColumnData baoCao = new ColumnData("xuatbaocao", 1, group.XuatBaoCao.ToString());
            ColumnData ht_Them = new ColumnData("ht_them", 1, group.HT_Them.ToString());
            ColumnData ht_Sua = new ColumnData("ht_sua", 1, group.HT_Sua.ToString());
            ColumnData ht_Xoa = new ColumnData("ht_xoa", 1, group.HT_Xoa.ToString());
            ColumnData dm_Them = new ColumnData("danhmuc_them", 1, group.DanhMuc_Them.ToString());
            ColumnData dm_Sua = new ColumnData("danhmuc_sua", 1, group.DanhMuc_Sua.ToString());
            ColumnData dm_Xoa = new ColumnData("danhmuc_xoa", 1, group.DanhMuc_Xoa.ToString());
            ColumnData themKH = new ColumnData("themKH", 1, group.ThemKH.ToString());
            ColumnData danhsachKH = new ColumnData("danhsachKH", 1, group.DSKH.ToString());
            ColumnData suaKH = new ColumnData("suaKH", 1, group.SuaKH.ToString());
            ColumnData xoaKH = new ColumnData("xoaKH", 1, group.XoaKH.ToString());
            ArrayList columnData = new ArrayList();
            columnData.Add(maNhom);
            columnData.Add(tenNhom);
            columnData.Add(chuThich);
            columnData.Add(phanQuyen);
            columnData.Add(doiMatKhau);
            columnData.Add(congTy);
            columnData.Add(saoLuu);
            columnData.Add(phucHoi);
            columnData.Add(nhom);
            columnData.Add(dieuHanh);
            columnData.Add(quanLyXe);
            columnData.Add(laiXe);
            columnData.Add(tuyenXe);
            columnData.Add(xeThue);
            columnData.Add(doanhThu_Tuyen);
            columnData.Add(doanhthu_Thue);
            columnData.Add(taoHD);
            columnData.Add(baoCao);
            columnData.Add(ht_Them);
            columnData.Add(ht_Sua);
            columnData.Add(ht_Xoa);
            columnData.Add(dm_Them);
            columnData.Add(dm_Sua);
            columnData.Add(dm_Xoa);
            columnData.Add(themKH);
            columnData.Add(danhsachKH);
            columnData.Add(suaKH);
            columnData.Add(xoaKH);
            return myConnU.Insert("NHOM", columnData);
        }
        
       /* public int Insert(ArrayList groups)
        {
            int count = 0;
            foreach (GROUP g in groups)
            {
                count += this.Insert(g);
            }
            return count;
        }
        */

       public int Update(GROUP group, String condition)
        {
            ColumnData tenNhom = new ColumnData("tennhom", 1, group.TenNhom.ToString());
            ColumnData chuThich = new ColumnData("chuthich", 1, group.ChuThich.ToString());
            ColumnData phanQuyen = new ColumnData("phanquyen", 1, group.PhanQuyen.ToString());
            ColumnData doiMatKhau = new ColumnData("doimatkhau", 1, group.DoiMatKhau.ToString());
            ColumnData congTy = new ColumnData("congty", 1, group.CongTy.ToString());
            ColumnData saoLuu = new ColumnData("saoluu", 1, group.SaoLuu.ToString());
            ColumnData phucHoi = new ColumnData("phuchoi", 1, group.PhucHoi.ToString());
            ColumnData nhom = new ColumnData("nhom", 1, group.Nhom.ToString());
            ColumnData dieuHanh = new ColumnData("dieuhanh", 1, group.DieuHanh.ToString());
            ColumnData quanLyXe = new ColumnData("quanlyxe", 1, group.QuanLyXe.ToString());
            ColumnData laiXe = new ColumnData("laixe", 1, group.LaiXe.ToString());
            ColumnData tuyenXe = new ColumnData("tuyenxe", 1, group.TuyenXe.ToString());
            ColumnData xeThue = new ColumnData("xethue", 1, group.XeThue.ToString());
            ColumnData doanhThu_Tuyen = new ColumnData("doanhthu_xechaytuyen", 1, group.DoanhThu_XeChayTuyen.ToString());
            ColumnData doanhthu_Thue = new ColumnData("doanhthu_xethue", 1, group.DoanhThu_XeThue.ToString());
            ColumnData taoHD = new ColumnData("taohopdong", 1, group.TaoHopDong.ToString());
            ColumnData baoCao = new ColumnData("xuatbaocao", 1, group.XuatBaoCao.ToString());
            ColumnData ht_Them = new ColumnData("ht_them", 1, group.HT_Them.ToString());
            ColumnData ht_Sua = new ColumnData("ht_sua", 1, group.HT_Sua.ToString());
            ColumnData ht_Xoa = new ColumnData("ht_xoa", 1, group.HT_Xoa.ToString());
            ColumnData dm_Them = new ColumnData("danhmuc_them", 1, group.DanhMuc_Them.ToString());
            ColumnData dm_Sua = new ColumnData("danhmuc_sua", 1, group.DanhMuc_Sua.ToString());
            ColumnData dm_Xoa = new ColumnData("danhmuc_xoa", 1, group.DanhMuc_Xoa.ToString());
            ColumnData themKH = new ColumnData("themKH", 1, group.ThemKH.ToString());
            ColumnData danhsachKH = new ColumnData("danhsachKH", 1, group.DSKH.ToString());
            ColumnData suaKH = new ColumnData("suaKH", 1, group.SuaKH.ToString());
            ColumnData xoaKH = new ColumnData("xoaKH", 1, group.XoaKH.ToString());
            ArrayList columnData = new ArrayList();
            columnData.Add(tenNhom);
            columnData.Add(chuThich);
            columnData.Add(phanQuyen);
            columnData.Add(doiMatKhau);
            columnData.Add(congTy);
            columnData.Add(saoLuu);
            columnData.Add(phucHoi);
            columnData.Add(nhom);
            columnData.Add(dieuHanh);
            columnData.Add(quanLyXe);
            columnData.Add(laiXe);
            columnData.Add(tuyenXe);
            columnData.Add(xeThue);
            columnData.Add(doanhThu_Tuyen);
            columnData.Add(doanhthu_Thue);
            columnData.Add(taoHD);
            columnData.Add(baoCao);
            columnData.Add(ht_Them);
            columnData.Add(ht_Sua);
            columnData.Add(ht_Xoa);
            columnData.Add(dm_Them);
            columnData.Add(dm_Sua);
            columnData.Add(dm_Xoa);
            columnData.Add(themKH);
            columnData.Add(danhsachKH);
            columnData.Add(suaKH);
            columnData.Add(xoaKH);
            return myConnU.Update("NHOM", columnData, condition);
        }
       
        public int Update(ArrayList groups, String condition)
        {
            int count = 0;
            foreach (GROUP g in groups)
            {
                count += this.Update(g, condition);
            }
            return count;
        }

        public int Delete(String condition)
        {
            return myConnU.Delete("NHOM", condition);
        }
        
 //=============================================================================================
        //Hàm lấy giá trị lơn nhất (Mr.Tuyên K_54HTTT)
        public String getOrder()
        {
            string strID = myConnU.GetTopDataCell("NHOM", "maNhom");
            string strID1;
            string strID2;
            strID1 = strID.Substring(0, 2);
            strID2 = strID.Substring(2, 5);
            int autoNum = Convert.ToInt32(strID2) + 1;

            if (autoNum < 10)
                return strID1 + "0000" + autoNum;

            else if (autoNum >= 10 && autoNum < 100)
                return strID1 + "000" + autoNum;

            else if (autoNum >= 100 && autoNum < 1000)
                return strID1 + "00" + autoNum;
            else if (autoNum >= 1000 && autoNum < 10000)
                return strID1 + "0" + autoNum;
            else if (autoNum >= 10000 && autoNum < 100000)
                return strID1 + "" + autoNum;

            else
                return "";
        }

        //End hàm lấy giá trị lưn nhất (Mr.Tuyên K_54HTTT)
//=============================================================================================
    }
}
